﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EcSalseManage.App_Code;
using System.IO;
using System.Net;

namespace EcSalseManage.EcSalse
{
    public partial class TestWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            //DataEntity.T_Project Project = new DataEntity.T_Project();
            //Project.Project_NO = Project_Inherit.genProjectNO();
            //Project.ProjectNOIdx = Project_Inherit.genProjectIndex().ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            #region 上传附件部分

            try
            {
                DataAccess.Operate.DA_Report_Attach_Operate Report_Attach_Operate = new DataAccess.Operate.DA_Report_Attach_Operate();
                //string strFiles = this.HiddenField2.Value.Substring(0, this.HiddenField2.Value.Length - 1);
                //string delimStr = "|";
                //char[] delimiter = delimStr.ToCharArray();
                //string[] split = strFiles.Split(delimiter, 4);
                //for (int i = 0; i < split.Length; i++)
                //{
                DataEntity.T_Report_Attach Report_Attach = new DataEntity.T_Report_Attach();
                Report_Attach.Project_Report_MainID = new Guid("C1844839-F1E3-419B-ABAD-6D30FAADF0F2");
                //delimStr = ",";
                //delimiter = delimStr.ToCharArray();
                //string[] item = split[i].ToString().Split(delimiter, 3);
                string sFileName = "";
                string sFilePath = "";
                string sFileType = "";
                if (!Directory.Exists("\\\\gzs-ecc02\\ReportManager\\" + DateTime.Now.Year.ToString()))
                {
                    Directory.CreateDirectory("\\\\gzs-ecc02\\ReportManager\\" + DateTime.Now.Year.ToString());
                }
                if (!Directory.Exists("\\\\gzs-ecc02\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + "C1844839-F1E3-419B-ABAD-6D30FAADF0F2"))
                {
                    Directory.CreateDirectory("\\\\gzs-ecc02\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + "C1844839-F1E3-419B-ABAD-6D30FAADF0F2");
                }
                int fileType = 1;
                sFileType = ".jpg";
                sFileName = "C1844839-F1E3-419B-ABAD-6D30FAADF0F2" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + sFileType;
                sFilePath = "\\ReportManager\\" + DateTime.Now.Year + "\\" + "C1844839-F1E3-419B-ABAD-6D30FAADF0F2" + "\\" + sFileName;
                string uriString = "\\\\gzs-ecc02\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + "C1844839-F1E3-419B-ABAD-6D30FAADF0F2" + "\\" + sFileName;
                this.txtFilePath.Value = @"C:\Users\tianchao.CENTALINE\Pictures\1_vrhero.jpg";
               
                //UpLoadFile(@"C:\Users\tianchao.CENTALINE\Pictures\1_vrhero.jpg", uriString);
                this.txtFilePath.PostedFile.SaveAs("\\\\gzs-ecc02\\ReportManager\\" + DateTime.Now.Year.ToString() + "\\" + "C1844839-F1E3-419B-ABAD-6D30FAADF0F2" + "\\" + sFileName);

                //Report_Attach.Project_Report_FileID = Convert.ToInt32(item[0]);
                //Report_Attach.Project_Report_ExchangeTypeID = 11;
                //Report_Attach.Project_Report_FilePath = sFilePath;
                //Report_Attach.Project_Report_FileDesc = item[2];
                //Report_Attach.Project_Report_FileName = sFileName;
                //Report_Attach_Operate.Insert(Report_Attach);

                //DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 5, "C1844839-F1E3-419B-ABAD-6D30FAADF0F2", 4);
                //}
            #endregion
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
            finally
            {
                Response.Write("<script>alert('ok');</script>");
            }

        }


        #region WebClient上传附件
        /// <summary>
        /// WebClient上传文件至服务器
        /// </summary>
        /// <param name="fileNamePath">文件名，全路径格式</param>
        /// <param name="uriString">服务器文件夹路径</param>
        public void UpLoadFile(string fileNamePath, string uriString)
        {

            try
            {
                /**/
                /// 创建WebClient实例 
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = CredentialCache.DefaultCredentials;
                // 要上传的文件 
                FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
                // FileStream fs = OpenFile(); 
                BinaryReader r = new BinaryReader(fs);
                byte[] postArray = r.ReadBytes((int)fs.Length);
                Stream postStream = myWebClient.OpenWrite(uriString, " PUT ");
                try
                {

                    // 使用UploadFile方法可以用下面的格式
                    // myWebClient.UploadFile(uriString,"PUT",fileNamePath); 


                    if (postStream.CanWrite)
                    {
                        postStream.Write(postArray, 0, postArray.Length);
                        postStream.Close();
                        fs.Dispose();
                    }
                    else
                    {
                        postStream.Close();
                        fs.Dispose();

                    }
                }
                catch (Exception err)
                {
                    postStream.Close();
                    fs.Dispose();
                    // Utility.LogWriter log = new Utility.LogWriter(); 
                    throw err;
                }
                finally
                {
                    postStream.Close();
                    fs.Dispose();
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        }
        #endregion

     
    }
}
